package com.ca.ridersclubapplication.view.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.util.NumberUtils;

public class IconButton extends ConstraintLayout {

    private Drawable icon = null;
    private Drawable activeIcon = null;
    private Long value = 0L;
    private boolean active = false;

    public ImageView iconView;
    public TextView valueView;

    public IconButton(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public IconButton(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public IconButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.icon_button_layout, this);
        iconView = view.findViewById(R.id.iv_icon);
        valueView = view.findViewById(R.id.tv_value);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.IconButton, 0, 0);
            icon = array.getDrawable(R.styleable.IconButton_ib_icon);
            activeIcon = array.getDrawable(R.styleable.IconButton_ib_icon_active);
            value = (long) array.getInteger(R.styleable.IconButton_ib_value, 0);
            active = array.getBoolean(R.styleable.IconButton_ib_active, false);

            array.recycle();
        }

        setIcon(icon);
        setActiveIcon(activeIcon);
        setValue(value);
        setActive(active);

        setOnClickListener(v -> {});
    }

    public void setActive(boolean active) {
        this.active = active;
        iconView.setImageDrawable(this.active ? activeIcon : icon);
        valueView.setTextColor(
                this.active ? getContext().getResources().getColor(R.color.primary)
                : getContext().getResources().getColor(R.color.secondary_text));
    }

    public boolean isActive() {
        return active;
    }

    public void setValue(Long value) {
        this.value = value == null ? 0L : value;
        valueView.setText(NumberUtils.numberToString(value));
    }

    public Long getValue() {
        return value;
    }

    public void setIcon(Drawable icon) {
        this.icon = icon;
        iconView.setImageDrawable(this.active ? activeIcon : icon);
    }

    public void setActiveIcon(Drawable activeIcon) {
        this.activeIcon = activeIcon;
        iconView.setImageDrawable(this.active ? activeIcon : icon);
    }
}
